package song.NO_21;

public class Solution {
    private Lianbiao lianbiao1;
    private Lianbiao lianbiao2;
    private String s1;
    private String s2;
    private Lianbiao newlianbiao;
    public Solution(){
        this.lianbiao1 = null;
        this.lianbiao2 = null;
        this.newlianbiao = null;
        this.s1 = "";
        this.s2 = "";
    }
    public Solution(final String s1, final String s2){
        this.newlianbiao = new Lianbiao();
        this.s1 = s1;
        this.lianbiao1 = new Lianbiao(this.s1);
        this.lianbiao1.lianbiao_generating();
        this.s2 = s2;
        this.lianbiao2 = new Lianbiao(this.s2);
        this.lianbiao2.lianbiao_generating();
    }

    public void combining(){
        /**
         * 对两个有序链表按元素从小到大的顺序合并
         */
        var move1 = this.lianbiao1.getHead().next_return();
        var move2 = this.lianbiao2.getHead().next_return();
        var move = this.newlianbiao;
        while (move1 != null && move2 != null){
            if (Integer.compare(move1.num_return(), move2.num_return()) <= 0){
                var node_new = new Node(move1.num_return());
                move.lianbiao_append(node_new);
//                move1.setNext(move1.next_return().next_return());
                move1 = move1.next_return();
            }
            else{
                var node_new = new Node(move2.num_return());
                move.lianbiao_append(node_new);
//                move2.setNext(move2.next_return().next_return());
                move2 = move2.next_return();
            }
        }
        move.lianbiao_append(move1 == null ? move2 : move1);
    }
    public void newlianbiao_print(){
        this.newlianbiao.lianbiao_print();
    }

    public static void main(String[] args){
        String s1 = "1->2->4";
        String s2 = "1->3->4";
        Solution solution = new Solution(s1, s2);
        solution.combining();
        solution.newlianbiao_print();
    }
}
